<%@page import="com.ddd.entity.Negotiation"%>
<%@page import="com.ddd.context.Context"%>
<%@page import="com.ddd.service.NegotiationService"%>
<%@page import="com.ddd.util.Util"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.ddd.service.PublisherService"%>
<%@page import="com.ddd.context.Parameters"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%
    Util.checkBEAuthorized(request, response);

    String message = request.getParameter("message");
    String gotoPage = request.getParameter("gotoPage");

    Parameters para = new Parameters(request);
    if (Util.isNotBlank(gotoPage)) {
        para.setCurrentPage(Integer.parseInt(gotoPage));
    } else {
        para.setCurrentPage(1);
    }

    // load data
    // 必須最後load, 因為會將筆數寫入session
    NegotiationService negotiationService = new NegotiationService(request);
    ArrayList negoList = negotiationService.loadAll();
    
    
    //搜尋功能
    ArrayList negoList_s = new ArrayList();
    String temp4=""; //查詢選擇的欄位value
    String temp3=""; //查詢的文字
    String temp2=""; //暫時抽出查詢欄位的值以便比對
    //temp1 是將整列的資料倒入以便比對分析
    
    for (int i=0;i<negoList.size();i++) {
        Negotiation temp1=(Negotiation) negoList.get(i); 
        
        if(request.getParameter("search_f")!=""&&request.getParameter("search_f")!=null){
            temp4=request.getParameter("search_f");
            //人員姓名 期刊名稱 簽約成功率
                if(temp4.contains("a")){
                    if(temp1.getStaff()!=null){
                        temp2=temp1.getStaff();}
                    else{
                        temp2="abcdefg";}
                    %>
                    <%//=temp2%>
                    <%
                }
                else if(temp4.contains("b")){
                    if(temp1.getJournalName()!=null){
                        temp2=temp1.getJournalName();}
                    else{
                        temp2="abcdefg";}
                }
                else if(temp4.contains("c")){
                    if(temp1.getSuccessRate()!=null){
                    temp2=temp1.getSuccessRate();}
                    else{
                        temp2="abcdefg";}                    
                }
        }
        if(request.getParameter("search_value")!=""&&request.getParameter("search_value")!=null){
            temp3=request.getParameter("search_value");
            if(temp2.contains(temp3)){
                negoList_s.add(negoList.get(i));
                    %>
                    <%//=temp2+" @@ "%>
                    <%
            }
        }else{ 
            if(temp4.contains("c")){
                temp3=request.getParameter("search_value1");
                if(temp2.contains(temp3)){
                    negoList_s.add(negoList.get(i));}
            }else{
                negoList_s.add(negoList.get(i));
            }
        }     
    }
    para.setTotalSize(negoList_s.size());
    //搜尋End
%>

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title><%=Context.SITE_NAME%></title>

        <link type="text/css" href="css/style.css" rel="stylesheet" />
        <link type="text/css" href="css/ui-lightness/jquery-ui-1.8.16.custom.css" rel="stylesheet" />
        <script type="text/javascript" src="js/jquery-1.6.2.min.js"></script>
        <script type="text/javascript" src="js/jquery-ui-1.8.16.custom.min.js"></script>
        <script type="text/javascript">
            function doSelect(){
               $('#form2').submit();
            }
            
            function doUpdate (id){
                window.location = "negotiationdetails.jsp?mode=update&id=" + id;
            }
            
            function doDelete (id){
                $( "#dialog_delete" ).dialog({
                    title: "刪除版權徵集",
                    resizable: false,
                    height:140,
                    modal: true,
                    buttons: {
                        "刪除": function() {
                            $( this ).dialog( "close" );
                            window.location = "../doNegotiation?mode=delete&id=" + id;
                        },
                        "取消": function() {
                            $( this ).dialog( "close" );
                        }
                    }
                });
            }
            
            function gotoPage(page){
                window.location = "negotiation.jsp?gotoPage=" + page;
            }
            
            function showMessage(){
            <%if (Util.isNotBlank(message)) {%>
                    $( "#dialog_message" ).dialog({
                        modal: true,
                        title:"訊息",
                        buttons: {
                            Ok: function() {
                                $( this ).dialog( "close" );
                            }
                        }
                    });
            <%}%>
                }
            
            
            function change_select_f(s) 
            { 
                if(s=='c')
                    {
                        document.getElementById('search_value').style.display = 'none';
                        document.getElementById('search_value1').style.display = 'inline';
                        search_value.value="";
                    }
                if(s=='a' || s=='b')
                    {
                        document.getElementById('search_value1').style.display = 'none';
                        document.getElementById('search_value').style.display = 'inline';
                        search_value.value="";
                    }
            } 
        </script>
    </head>
    <body onload="showMessage();">
        <div id="wrapper">
            <div id="header">
                <div id="logo">
                    <h1><a href="#"><%=Context.SITE_NAME%></a></h1>
                </div>
            </div>
            <!-- end #header -->
            <div id="menu">
                <%@ include file="menu.jsp" %>
            </div>
            <!-- end #menu -->
            <div id="page">
                <div id="page-bgtop">
                    <div id="page-bgbtm">
                        <form id="form2" name="form2" action="/DDD/BE/negotiation.jsp" method="post">
                                搜尋：
                                <!-- 人員姓名 期刊名稱 簽約成功率 -->
                                <select name="search_f" id="search_f" onchange="change_select_f(this.options[this.options.selectedIndex].value)">
                                    <option value="a" <%=(temp4.contains("a"))?"selected=\"selected\"":""%>>人員姓名</option>
                                    <option value="b" <%=(temp4.contains("b"))?"selected=\"selected\"":""%>>期刊名稱</option>
                                    <option value="c" <%=(temp4.contains("c"))?"selected=\"selected\"":""%>>簽約成功率</option>
                                </select>
                                
                                <input type="text" name="search_value" id="search_value" value="<%=((temp4.contains("c")))?"":temp3%>" style="<%=((temp4.contains("c")))?"display:none":""%>"/>
                                <select style="<%=(temp4.contains("c"))?"":"display:none"%>" id="search_value1" name="search_value1">
                                    <option value=""></option>
                                    <% for(int ii=0;ii<101;ii=ii+10){%>
                                    <!-- 加selected -->
                                    <option value="<%=ii%>" <%=(temp3.contains(Integer.toString(ii)))?"selected":""%> ><%=ii%> %</option>
                                    <% } %>
                                </select>
                                <input type="button" value="搜尋" name="search_submit" id="search_submit" onclick="doSelect()"/>
                        </form>
                        <!-- starrt #content -->
                        <div id="content">
                            <p>
                                <a href="negotiationdetails.jsp?mode=add">新增版權徵集</a>
                            </p>
                            <table>
                                <thead>
                                    <tr>
                                        <th>人員姓名</th>
                                        <th>簽約單位</th>
                                        <th>期刊名稱</th>
                                        <th>拜訪日期</th>
                                        <th>簽約成功率</th>
                                        <th>修改/合作紀錄</th>
                                        <th>刪除</th>
                                    </tr>
                                </thead>
                                <%
                                    int left = para.getPageSize();
                                    for (int i = (para.getCurrentPage() - 1) * para.getPageSize(); i < para.getCurrentPage() * para.getPageSize() && i < para.getTotalSize(); i++) {
                                        left--;
                                        Negotiation n = (Negotiation) negoList_s.get(i);
                                %>
                                <tr>
                                    <td><%=n.getStaff()%></td>
                                    <td><%=n.getAuthority()%></td>
                                    <td><%=n.getJournalName()%></td>
                                    <td><%=n.getVisitDate()%></td>
                                    <td><%=n.getSuccessRate()%></td>
                                    <td><a href='javascript:doUpdate("<%=n.getId()%>");'>修改/合作紀錄</a></td>
                                    <td><a href='javascript:doDelete("<%=n.getId()%>");'>刪除</a></td>
                                </tr>
                                <%}%>

                                <%for (int i = 0; i < left; i++) {%>
                                <tr>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                    <td>&nbsp;</td>
                                </tr>
                                <%}%>
                            </table>
                            <br /> 
                            <p align="center">
                                <%=para.getCurrentPage()%> / <%=para.getTotalPage()%> (總筆數: <%=para.getTotalSize()%>)
                            </p>
                            <p align="center">
                                <%if (para.getCurrentPage() > 1) {%>
                                <input type="button" value="上一頁" onclick='gotoPage(<%=para.getCurrentPage() - 1%>)'>
                                <%}%>
                                <%if (para.getCurrentPage() < para.getTotalPage()) {%>
                                <input type="button" value="下一頁" onclick='gotoPage(<%=para.getCurrentPage() + 1%>)'>
                                <%}%>
                            </p>

                        </div>
                        <!-- end #content -->

                        <!-- end #sidebar -->
                        <div style="clear: both;">&nbsp;</div>
                    </div>
                </div>
            </div>
            <!-- end #page -->
        </div>
        <div id="footer">
            <p><%=Context.FOOTER%></p>
        </div>
        <!-- end #footer -->
        <!-- ui-dialog -->
        <div id="dialog_delete" style="display: none">
            <p>是否要刪除版權徵集？</p>
        </div>

        <div id="dialog_message" style="display: none;">
            <p><%=message%></p>
        </div>

    </body>
</html>
